﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;

namespace DBProvider.Providers
{
	public class QuestionProvider : IQuestionProvider
	{
		#region IQuestionProvider Members

		public List<QuestionCategory> GetAllQuestionCategory()
		{
			List<QuestionCategory> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					result = db.QuestionCategories.ToList();
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public QuestionCategory CreateQuestionCategory(QuestionCategory theNewQuestionCat, out string msg)
		{
			if(theNewQuestionCat == null)
			{
				throw new ArgumentNullException();
			}

			msg = string.Empty;
			QuestionCategory result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var isExist = db.QuestionCategories.Count(qc => qc.QuestionCategoryName == theNewQuestionCat.QuestionCategoryName) > 0;

					if(isExist)
					{
						msg = "同名的问题类别已存在";
					}
					else
					{
						db.QuestionCategories.InsertOnSubmit(theNewQuestionCat);
						db.SubmitChanges();

						result = db.QuestionCategories.SingleOrDefault(qc => qc.QuestionCategoryName == theNewQuestionCat.QuestionCategoryName);
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public bool DeleteQuestionCategory(int theQuestionCatID)
		{
			if(theQuestionCatID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			var result = false;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var dbQuestionCat = db.QuestionCategories.SingleOrDefault(qc => qc.QuestionCategoryID == theQuestionCatID);

					if(dbQuestionCat != null)
					{
						db.QuestionCategories.DeleteOnSubmit(dbQuestionCat);
						db.SubmitChanges();

						result = true;
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public bool UpdateQuestionCategory(QuestionCategory theQuestionCat)
		{
			if(theQuestionCat == null || theQuestionCat.QuestionCategoryID <= 0)
			{
				throw new ArgumentException();
			}

			var result = false;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var dbQuestionCat = db.QuestionCategories.SingleOrDefault(qc => qc.QuestionCategoryID == theQuestionCat.QuestionCategoryID);

					if(dbQuestionCat != null)
					{
						dbQuestionCat.QuestionCategoryName = theQuestionCat.QuestionCategoryName;
						dbQuestionCat.QuestionCategoryDescription = theQuestionCat.QuestionCategoryDescription;

						db.SubmitChanges();

						result = true;
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Question> GetQuestionByCategory(int catID, int drugStoreID)
		{
			if(catID <= 0 || drugStoreID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Question> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var data = from q in db.Questions
										 where q.QuestionCategoryID == catID && q.DrugStoreID == drugStoreID
										 select q;

					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Question> GetQuestionByCategory(int catID, int pageIndex, int pageSize, out int totalRecords, int drugStoreID)
		{
			if(catID <= 0 || drugStoreID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Question> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					totalRecords = db.Questions.Count(q => q.QuestionCategoryID == catID);

					var data = (from q in db.Questions
											where q.QuestionCategoryID == catID && q.DrugStoreID == drugStoreID
											select q).Skip(pageIndex * pageSize).Take(pageSize);

					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public Question GetQuestion(int theQuestionID)
		{
			if(theQuestionID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			Question result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					DataLoadOptions dlo = new DataLoadOptions();
					dlo.LoadWith<Question>(q => q.User);
					dlo.LoadWith<Question>(q => q.Answers);
					db.LoadOptions = dlo;

					result = db.Questions.SingleOrDefault(q => q.QuestionID == theQuestionID);
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public Question CreateQuestion(Question theNewQuestion, out string msg)
		{
			if(theNewQuestion == null)
			{
				throw new ArgumentNullException();
			}

			msg = string.Empty;
			Question result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var isExist = db.Questions.Count(q => q.QuestionContent == theNewQuestion.QuestionContent) > 0;

					if(isExist)
					{
						msg = "问题已存在";
					}
					else
					{
						db.Questions.InsertOnSubmit(theNewQuestion);
						db.SubmitChanges();

						result = db.Questions.SingleOrDefault(q => q.QuestionContent == theNewQuestion.QuestionContent);
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public bool DeleteQuestion(int theQuestionID)
		{
			if(theQuestionID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			var result = false;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var tagSubmit = false;

					var dbAnswer = db.Answers.Where(a => a.QuestionID == theQuestionID);

					if(dbAnswer != null)
					{
						db.Answers.DeleteAllOnSubmit(dbAnswer);
						tagSubmit = true;
					}

					var dbQuestionAndShareTo = db.RLT_QuestionAndShareTos.Where(qs => qs.QuestionID == theQuestionID);

					if(dbQuestionAndShareTo != null)
					{
						db.RLT_QuestionAndShareTos.DeleteAllOnSubmit(dbQuestionAndShareTo);
						tagSubmit = true;
					}

					var dbQuestion = db.Questions.SingleOrDefault(q => q.QuestionID == theQuestionID);

					if(dbQuestion != null)
					{
						db.Questions.DeleteOnSubmit(dbQuestion);
						tagSubmit = true;
					}

					if(tagSubmit)
					{
						db.SubmitChanges();

						result = true;
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public bool UpdateQuestion(Question theQuestion)
		{
			if(theQuestion == null || theQuestion.QuestionID <= 0)
			{
				throw new ArgumentException();
			}

			var result = false;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var dbQuestion = db.Questions.SingleOrDefault(q => q.QuestionID == theQuestion.QuestionID);

					if(dbQuestion != null)
					{
						dbQuestion.Clone(theQuestion);
						db.SubmitChanges();

						result = true;
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Answer> GetAnswerByQuestion(int theQuestionID)
		{
			if(theQuestionID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Answer> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					DataLoadOptions dlo = new DataLoadOptions();
					dlo.LoadWith<Answer>(a => a.User);
					db.LoadOptions = dlo;

					var data = from a in db.Answers
										 where a.QuestionID == theQuestionID
										 select a;

					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public Answer CreateAnswer(Answer theNewAnswer)
		{
			if(theNewAnswer == null)
			{
				throw new ArgumentNullException();
			}

			Answer result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					db.Answers.InsertOnSubmit(theNewAnswer);
					db.SubmitChanges();

					result = db.Answers.SingleOrDefault(a => a.AnswerContent == theNewAnswer.AnswerContent && a.QuestionID == theNewAnswer.QuestionID && a.AnswerTime == theNewAnswer.AnswerTime);
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public bool DeleteAnswer(int theAnswerID)
		{
			if(theAnswerID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			var result = false;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var dbAnswer = db.Answers.SingleOrDefault(a => a.AnswerID == theAnswerID);

					if(dbAnswer != null)
					{
						db.Answers.DeleteOnSubmit(dbAnswer);
						db.SubmitChanges();

						result = true;
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public bool UpdateAnswer(Answer theAnswer)
		{
			if(theAnswer == null || theAnswer.AnswerID <= 0)
			{
				throw new ArgumentException();
			}

			var result = false;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var dbAnswer = db.Answers.SingleOrDefault(a => a.AnswerID == theAnswer.AnswerID);

					if(dbAnswer != null)
					{
						dbAnswer.AnswerContent = theAnswer.AnswerContent;
						dbAnswer.AnswerTime = theAnswer.AnswerTime;
						dbAnswer.AnswerUserID = theAnswer.AnswerUserID;
						dbAnswer.IsTheBestAnswer = theAnswer.IsTheBestAnswer;
						dbAnswer.QuestionID = theAnswer.QuestionID;

						db.SubmitChanges();

						result = true;
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Question> GetQuestionByState(QuestionState questionState, int questionCatID, int drugStoreID)
		{
			if(drugStoreID <= 0)
			{
				throw new ArgumentException();
			}

			List<Question> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					if(questionCatID > 0)
					{
						var data = from q in db.Questions
											 where q.QuestionState == (int)questionState && q.QuestionCategoryID == questionCatID && q.DrugStoreID == drugStoreID
											 orderby q.QuestionAddTime descending
											 select q;

						if(data != null)
						{
							result = data.ToList();
						}
					}
					else
					{
						var data = from q in db.Questions
											 where q.QuestionState == (int)questionState && q.DrugStoreID == drugStoreID
											 orderby q.QuestionAddTime descending
											 select q;

						if(data != null)
						{
							result = data.ToList();
						}
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Question> GetQuestionByUser(int userID, int questionCatID)
		{
			if(userID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Question> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					if(questionCatID > 0)
					{
						var data = from q in db.Questions
											 where q.QuestionUserID == userID && q.QuestionCategoryID == questionCatID
											 orderby q.QuestionAddTime descending
											 select q;

						if(data != null)
						{
							result = data.ToList();
						}
					}
					else
					{
						var data = from q in db.Questions
											 where q.QuestionUserID == userID
											 orderby q.QuestionAddTime descending
											 select q;

						if(data != null)
						{
							result = data.ToList();
						}
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Question> GetHaveAnsweredQuestionByUser(int userID, int questionCatID)
		{
			if(userID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Question> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					if(questionCatID > 0)
					{
						var data = from q in db.Questions
											 where q.QuestionCategoryID == questionCatID && (from a in db.Answers
																																			 where a.AnswerUserID == userID
																																			 select a.QuestionID).Contains(q.QuestionID)
											 orderby q.QuestionAddTime descending
											 select q;

						if(data != null)
						{
							result = data.ToList();
						}
					}
					else
					{
						var data = from q in db.Questions
											 where (from a in db.Answers
															where a.AnswerUserID == userID
															select a.QuestionID).Contains(q.QuestionID)
											 select q;

						if(data != null)
						{
							result = data.ToList();
						}
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Question> GetNewQuestions(int count)
		{
			if(count <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Question> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					DataLoadOptions dlo = new DataLoadOptions();
					dlo.LoadWith<Question>(q => q.User);
					db.LoadOptions = dlo;

					var data = from q in db.Questions
										 where q.QuestionState != (int)QuestionState.Resolved
										 orderby q.QuestionAddTime descending
										 select q;

					if(data != null)
					{
						result = data.Take(count).ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Question> GetNewQuestions(int count, int drugStoreID)
		{
			if(count <= 0 || drugStoreID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Question> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					DataLoadOptions dlo = new DataLoadOptions();
					dlo.LoadWith<Question>(q => q.User);
					db.LoadOptions = dlo;

					var data = from q in db.Questions
										 where q.DrugStoreID == drugStoreID && q.QuestionState != (int)QuestionState.Resolved
										 orderby q.QuestionAddTime descending
										 select q;

					if(data != null)
					{
						result = data.Take(count).ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Question> GetQuestionByState(QuestionState questionState, int questionCatID, int drugStoreID, int pageIndex, int pageSize, out int totalRecords)
		{
			if(drugStoreID <= 0 || pageIndex < 0 || pageSize < 0)
			{
				throw new ArgumentException();
			}

			List<Question> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					if(questionCatID > 0)
					{
						totalRecords = db.Questions.Count(q => q.QuestionState == (int)questionState && q.QuestionCategoryID == questionCatID && q.DrugStoreID == drugStoreID);

						var data = (from q in db.Questions
												where q.QuestionState == (int)questionState && q.QuestionCategoryID == questionCatID && q.DrugStoreID == drugStoreID
												orderby q.QuestionAddTime descending
												select q).Skip(pageIndex * pageSize).Take(pageSize);

						if(data != null)
						{
							result = data.ToList();
						}
					}
					else
					{
						totalRecords = db.Questions.Count(q => q.QuestionState == (int)questionState && q.DrugStoreID == drugStoreID);

						var data = (from q in db.Questions
												where q.QuestionState == (int)questionState && q.DrugStoreID == drugStoreID
												orderby q.QuestionAddTime descending
												select q).Skip(pageIndex * pageSize).Take(pageSize);

						if(data != null)
						{
							result = data.ToList();
						}
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Question> GetHaveAnsweredQuestionByUser(int userID, int questionCatID, int pageIndex, int pageSize, out int totalRecords)
		{
			if(userID <= 0 || pageSize < 0 || pageIndex < 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Question> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					if(questionCatID > 0)
					{
						totalRecords = (from q in db.Questions
														where q.QuestionCategoryID == questionCatID && (from a in db.Answers
																																						where a.AnswerUserID == userID
																																						select a.QuestionID).Contains(q.QuestionID)
														select q).Count();

						var data = (from q in db.Questions
												where q.QuestionCategoryID == questionCatID && (from a in db.Answers
																																				where a.AnswerUserID == userID
																																				select a.QuestionID).Contains(q.QuestionID)
												orderby q.QuestionAddTime descending
												select q).Skip(pageIndex * pageSize).Take(pageSize);

						if(data != null)
						{
							result = data.ToList();
						}
					}
					else
					{
						totalRecords = (from q in db.Questions
														where (from a in db.Answers
																	 where a.AnswerUserID == userID
																	 select a.QuestionID).Contains(q.QuestionID)
														select q).Count();

						var data = (from q in db.Questions
												where (from a in db.Answers
															 where a.AnswerUserID == userID
															 select a.QuestionID).Contains(q.QuestionID)
												orderby q.QuestionAddTime descending
												select q).Skip(pageIndex * pageSize).Take(pageSize);

						if(data != null)
						{
							result = data.ToList();
						}
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Question> GetQuestionByUser(int userID, int questionCatID, int pageIndex, int pageSize, out int totalRecords)
		{
			if(userID <= 0 || pageIndex < 0 || pageSize < 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Question> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					if(questionCatID > 0)
					{
						totalRecords = db.Questions.Count(q => q.QuestionUserID == userID && q.QuestionCategoryID == questionCatID);

						var data = (from q in db.Questions
												where q.QuestionUserID == userID && q.QuestionCategoryID == questionCatID
												orderby q.QuestionAddTime descending
												select q).Skip(pageIndex * pageSize).Take(pageSize);

						if(data != null)
						{
							result = data.ToList();
						}
					}
					else
					{
						totalRecords = db.Questions.Count(q => q.QuestionUserID == userID);

						var data = (from q in db.Questions
												where q.QuestionUserID == userID
												orderby q.QuestionAddTime descending
												select q).Skip(pageIndex * pageSize).Take(pageSize);

						if(data != null)
						{
							result = data.ToList();
						}
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<RLT_QuestionAndShareTo> GetQuestionAndShareToByQuestion(int questionID)
		{
			if(questionID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<RLT_QuestionAndShareTo> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					DataLoadOptions dlo = new DataLoadOptions();
					dlo.LoadWith<RLT_QuestionAndShareTo>(qs => qs.User);
					db.LoadOptions = dlo;

					var data = from qs in db.RLT_QuestionAndShareTos
										 where qs.QuestionID == questionID
										 select qs;

					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public void CreateQuestionAndShareTo(List<RLT_QuestionAndShareTo> lstQuestionAndShareTo)
		{
			if(lstQuestionAndShareTo == null || lstQuestionAndShareTo.Count <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					db.RLT_QuestionAndShareTos.InsertAllOnSubmit(lstQuestionAndShareTo);
					db.SubmitChanges();
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}
		}

		public void SaveQuestionAndShareTo(int questionID, List<RLT_QuestionAndShareTo> lstQuestionAndShareTo)
		{
			if(questionID <= 0 || lstQuestionAndShareTo == null)
			{
				throw new ArgumentOutOfRangeException();
			}

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var dbQuestionAndShareTo = from qs in db.RLT_QuestionAndShareTos
																		 where qs.QuestionID == questionID
																		 select qs;

					if(dbQuestionAndShareTo != null)
					{
						var lstDBQuestionAndShareTo = dbQuestionAndShareTo.ToList();

						var deleteItems = lstDBQuestionAndShareTo.Except(lstQuestionAndShareTo);

						if(deleteItems != null)
						{
							db.RLT_QuestionAndShareTos.DeleteAllOnSubmit(deleteItems);
						}

						var newItems = lstQuestionAndShareTo.Except(lstDBQuestionAndShareTo);

						if(newItems != null)
						{
							db.RLT_QuestionAndShareTos.InsertAllOnSubmit(newItems);
						}

						db.SubmitChanges();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}
		}

		public List<Question> GetQuestionByState(int userID, QuestionState questionState, int questionCatID, int drugStoreID)
		{
			if(drugStoreID <= 0)
			{
				throw new ArgumentException();
			}

			List<Question> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var user = db.Users.SingleOrDefault(u => u.UserID == userID);

					if(questionCatID > 0)
					{
						var data = from q in db.Questions
											 where q.QuestionState == (int)questionState && q.QuestionCategoryID == questionCatID && ((q.DrugStoreID == user.DrugStoreID) || (q.IsPublic == 1) || ((from qs in db.RLT_QuestionAndShareTos
																																																																																							where qs.ShareToDrugStoreID == user.DrugStoreID
																																																																																							select qs.QuestionID).Contains(q.QuestionID)) || ((from qs in db.RLT_QuestionAndShareTos
																																																																																																																 where qs.ShareToMemberID == userID
																																																																																																																 select qs.QuestionID).Contains(q.QuestionID)))
											 orderby q.QuestionAddTime descending
											 select q;

						if(data != null)
						{
							result = data.ToList();
						}
					}
					else
					{
						var data = from q in db.Questions
											 where q.QuestionState == (int)questionState && ((q.DrugStoreID == user.DrugStoreID) || (q.IsPublic == 1) || ((from qs in db.RLT_QuestionAndShareTos
																																																																		 where qs.ShareToDrugStoreID == user.DrugStoreID
																																																																		 select qs.QuestionID).Contains(q.QuestionID)) || ((from qs in db.RLT_QuestionAndShareTos
																																																																																												where qs.ShareToMemberID == userID
																																																																																												select qs.QuestionID).Contains(q.QuestionID)))
											 orderby q.QuestionAddTime descending
											 select q;

						if(data != null)
						{
							result = data.ToList();
						}
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Question> GetQuestionByState(int userID, QuestionState questionState, int questionCatID, int drugStoreID, int pageIndex, int pageSize, out int totalRecords)
		{
			if(drugStoreID <= 0 || pageIndex < 0 || pageSize < 0)
			{
				throw new ArgumentException();
			}

			List<Question> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var user = db.Users.SingleOrDefault(u => u.UserID == userID);

					if(questionCatID > 0)
					{
						totalRecords = db.Questions.Count(q => q.QuestionState == (int)questionState && q.QuestionCategoryID == questionCatID && q.DrugStoreID == drugStoreID);

						var data = (from q in db.Questions
												where q.QuestionState == (int)questionState && q.QuestionCategoryID == questionCatID && ((q.DrugStoreID == user.DrugStoreID) || (q.IsPublic == 1) || ((from qs in db.RLT_QuestionAndShareTos
																																																																																							 where qs.ShareToDrugStoreID == user.DrugStoreID
																																																																																							 select qs.QuestionID).Contains(q.QuestionID)) || ((from qs in db.RLT_QuestionAndShareTos
																																																																																																																	where qs.ShareToMemberID == userID
																																																																																																																	select qs.QuestionID).Contains(q.QuestionID)))
												orderby q.QuestionAddTime descending
												select q).Skip(pageIndex * pageSize).Take(pageSize);

						if(data != null)
						{
							result = data.ToList();
						}
					}
					else
					{
						totalRecords = db.Questions.Count(q => q.QuestionState == (int)questionState && q.DrugStoreID == drugStoreID);

						var data = (from q in db.Questions
												where q.QuestionState == (int)questionState && ((q.DrugStoreID == user.DrugStoreID) || (q.IsPublic == 1) || ((from qs in db.RLT_QuestionAndShareTos
																																																																			where qs.ShareToDrugStoreID == user.DrugStoreID
																																																																			select qs.QuestionID).Contains(q.QuestionID)) || ((from qs in db.RLT_QuestionAndShareTos
																																																																																												 where qs.ShareToMemberID == userID
																																																																																												 select qs.QuestionID).Contains(q.QuestionID)))
												orderby q.QuestionAddTime descending
												select q).Skip(pageIndex * pageSize).Take(pageSize);

						if(data != null)
						{
							result = data.ToList();
						}
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Question> GetNewQuestions(int count, int drugStoreID, int userID)
		{
			if(count <= 0 || drugStoreID <= 0 || userID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Question> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					DataLoadOptions dlo = new DataLoadOptions();
					dlo.LoadWith<Question>(q => q.User);
					db.LoadOptions = dlo;

					var user = db.Users.SingleOrDefault(u => u.UserID == userID);

					var data = from q in db.Questions
										 //where q.DrugStoreID == drugStoreID && q.QuestionState != (int)QuestionState.Resolved
										 where q.QuestionState != (int)QuestionState.Resolved && ((q.DrugStoreID == user.DrugStoreID) || (q.IsPublic == 1) || ((from qs in db.RLT_QuestionAndShareTos
																																																																						where qs.ShareToDrugStoreID == user.DrugStoreID
																																																																						select qs.QuestionID).Contains(q.QuestionID)) || ((from qs in db.RLT_QuestionAndShareTos
																																																																																															 where qs.ShareToMemberID == userID
																																																																																															 select qs.QuestionID).Contains(q.QuestionID)))
										 orderby q.QuestionAddTime descending
										 select q;

					if(data != null)
					{
						result = data.Take(count).ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		#endregion
	}
}
